﻿using G.Service.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace G.Service
{

    public abstract class BaseHandler : IHttpHandler
    {

        public bool IsReusable
        {
            get { return false; }
        }
        protected System.Collections.Specialized.NameValueCollection QueryString;
        public void ProcessRequest(HttpContext context)
        {
            BLL.IResult result = null;
            if (context.Request.HttpMethod.ToLower() == "post")
            {
                using (System.IO.StreamReader reader = new System.IO.StreamReader(context.Request.InputStream))
                {
                    string str = reader.ReadToEnd();
                    QueryString = new System.Collections.Specialized.NameValueCollection();
                    string[] kvp = str.Split('&');
                    foreach (var item in kvp)
                    {
                        string[] kv = item.Split('=');
                        if (kv.Length == 2)
                        {
                            QueryString[kv[0]] = HttpUtility.UrlDecode(kv[1]);
                        }
                    }
                }
            }
            else
                QueryString = context.Request.Params;
            string action = QueryString["action"];
            if (action == null)
                result = new BLL.Result() { Msg = "缺少参数action！" };
            else
            {
                try
                {
                    ProcessRequest(context, out result, action.ToLower());
                }
                catch (Exception ex)
                {
                    result = new Result() { IsOK = false, Msg = ex.Message };
                }
            }
            if (result != null)
                context.Response.Write(result.GetSerializaString());
        }
        protected abstract void ProcessRequest(HttpContext context, out IResult result, string action);
    }
    /// <summary>
    /// 处理BI统计的API
    /// </summary>
    public class APPService : BaseHandler
    {
        protected override void ProcessRequest(HttpContext context, out IResult result, string action)
        {
            switch (action)
            {
                case "addevent":
                    if (context.Request.HttpMethod.ToLower() == "post")
                    {
                        string appkey = QueryString["appkey"];
                        string eventid = QueryString["eventid"];
                        string eventcontent = QueryString["eventcontent"];
                        int eId = 0;
                        if (eventid == null)
                            result = new Result() { Msg = "eventid不能为空" };
                        else if (int.TryParse(eventid, out eId))
                            result = BLL.AppBLL.AddEvent(appkey, eId, eventcontent);
                        else
                            result = new Result() { Msg = "无效的eventid" };
                    }
                    else
                        result = new Result() { Msg = "请使用Post方法" };
                    break;
                default:
                    result = new BLL.Result() { Msg = "指定的action无效！" };
                    break;
            }
        }
    }
}